package capstoneproject.dudiachi.Main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import capstoneproject.dudiachi.Controller.PlaceManager;
import capstoneproject.dudiachi.Model.Place;

import com.viewpagerindicator.CirclePageIndicator;

public class PlaceActivity extends BaseActivity {

	private ViewPager mPager;
	private CirclePageIndicator mIndicator;
	private MyAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		BaseActivity.POS = 2;
		setContentView(R.layout.activity_home);
		
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		
		String tag = bundle.getString("tag");
		PlaceManager manager = new PlaceManager(getApplicationContext());
		ArrayList<Place> list = new ArrayList<Place>();
		if(tag.equals("search")){
			list = (ArrayList<Place>) intent.getSerializableExtra("list");
		}else{
			String id = bundle.getString("id");
			list = manager.searchByProvince(id);
		}
		
		mPager = (ViewPager)findViewById(R.id.viewPager);
		mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
		ArrayList<PlaceGridFragment> gridFragments = new ArrayList<PlaceGridFragment>();
		Iterator<Place> it = list.iterator();
		
		int i = 0;
		while (it.hasNext()) {
			ArrayList<GridItems> itemList = new ArrayList<GridItems>();
			ArrayList<Bitmap> bitArray = new ArrayList<Bitmap>();
			
			GridItems item = new GridItems(0, i);
			try {
				bitArray.add(GetBitmapFromAsset.getBitmapFromAssetGV(this, "Images/DiaDanh/" + list.get(i).getImage1()));
			} catch (IOException e) {
				e.printStackTrace();
			}
			itemList.add(item);
			i = i+1;
			it.next();
			
			for (int j = 1; j <= 8; j++) {
				if (it.hasNext()) {
					GridItems item1 = new GridItems(j, i);
					try {
						bitArray.add(GetBitmapFromAsset.getBitmapFromAssetGV(
								this, "Images/DiaDanh/" + list.get(i).getImage1()));
					} catch (IOException e) {
						e.printStackTrace();
					}
					itemList.add(item1);
					i = i + 1;
					it.next();
				}
			}
			GridItems[] gp = {};
			GridItems[] gridPage = itemList.toArray(gp);
			gridFragments.add(new PlaceGridFragment(gridPage, list, bitArray, PlaceActivity.this));
			
		}
		mAdapter = new MyAdapter(getSupportFragmentManager(), gridFragments);
		mPager.setAdapter(mAdapter);
		mIndicator.setViewPager(mPager);
	}

private class MyAdapter extends FragmentPagerAdapter{
		
		private List<PlaceGridFragment> list;
		
		public MyAdapter(android.support.v4.app.FragmentManager fm, List<PlaceGridFragment> list) {
			super(fm);
			this.list = list;
		}

		@Override
		public Fragment getItem(int pos) {
			return this.list.get(pos);
		}

		@Override
		public int getCount() {
			return this.list.size();
		}
	}
}
